<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="/js/jquery-3.5.0.min.js" type="text/javascript"></script>
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <link rel="stylesheet" href="/css/components.css">
    <link rel="stylesheet" href="/css/login.css">
    <title>商品详情</title>
</head>

<body class="login">
<div class="content">
    <h3>商品详情</h3>
    <div>
        <label class="control-label" for="item_title">商品名称</label>
        <h4 id="item_title"></h4>
        <label class="control-label" for="item_price">商品价格</label>
        <h4 id="item_price"></h4>

        <div>
            <img id=item_img width="280px" src="">
        </div>

        <h4 id="item_desciption"></h4>

        <label class="control-label" for="item_sales">销量</label>
        <h4 id="item_sales"></h4>

        <label class="control-label" for="item_stock">库存</label>
        <h4 id="item_stock"></h4>


        <div class="form-actions">
            <button class="btn blue" type="submit" id="seckill">点击购买</button>
        </div>
        <div class="form-actions" id="verifyDiv" style="display: none">
            <button class="btn blue" type="submit" style="color: white; background-color: #d84a38" id="verifyButton">
                验证
            </button>
            <img id="verifyImg" src="">
            <input type="text" id="verifyText">
        </div>

    </div>
</div>


</body>


<script>
    $(function () {
        var itemId = getId();
        var details = "";
        var uuidToken = window.localStorage["uuidToken"];
        var promoId = "";
        $.ajax({
            type: "GET",
            url: "/item/get_details?id=" + itemId,
            success: function (data) {
                if (data.status === "success") {
                    details = data.data;
                    promoId = details.promoId;
                    $("#item_img").attr("src", details.imgUrl);
                    $("#item_title").text(details.title);
                    $("#item_price").text(details.price);
                    $("#item_sales").text(details.sales);
                    $("#item_stock").text(details.stock);
                    if (data.data.promoStatus === 2) {
                        $("#seckill").before('<div id="promo" style="color: red"></div>');
                        $("#promo").text("该商品正于 " + data.data.promoStartDate + " 至 " + data.data.promoEndDate + " 间进行秒杀活动");
                        $("#seckill").attr('class', 'btn red');
                        $("#item_price").attr("style", "color:red");
                        $("#item_price").text("秒杀价：" + details.secPrice)
                    }

                } else {
                    alert(data.data.errorMsg)
                }
            },
            error: function (data) {
                alert("获取商品失败：" + data.responseText)
            }
        });

        $("#seckill").on("click", function () {
            if (uuidToken == null) {
                alert("用户尚未登录");
                window.location.href = "/html/login.html";
                return false;
            } else {
                // 秒杀商品的请求逻辑
                if (promoId != null) {
                    $("#verifyImg").attr("src", "/order/get_verify_code?uuidToken=" + uuidToken);
                    $("#verifyDiv").show();
                    $("#verifyButton").click(function () {
                        $.ajax({
                            type: "POST",
                            url: "/order/get_wand?uuidToken=" + uuidToken,
                            data: {
                                "item_id": itemId,
                                "amount": 1,
                                "promoId": promoId,
                                "verifyCode": $("#verifyText").val()
                            },
                            success: function (data) {
                                if (data.status === "success") {
                                    wand = data.data;
                                    $.ajax({
                                        type: "POST",
                                        url: "/order/create_order?uuidToken=" + uuidToken,
                                        data: {
                                            "item_id": itemId,
                                            "amount": 1,
                                            "promoId": details.promoId,
                                            "wand": "wand"
                                        }, // todo: amount暂时写死，先模拟秒杀单件商品的情况
                                        success: function (data) {
                                            if (data.status === "success") {
                                                alert("下单成功");
                                                window.location.reload()
                                            } else {
                                                alert(data.data.errorMsg);
                                                console.log(data.data.errorCode);
                                                if (data.data.errorCode === 10004) {
                                                    window.location.href = "/html/login.html"
                                                }
                                            }
                                        },
                                        error: function (data) {
                                            alert("下单失败：" + data.responseText)
                                        }
                                    })
                                } else {
                                    alert("获取令牌失败1：" + data.data.errorMsg)
                                }
                            },
                            error: function (data) {
                                alert("获取令牌失败2：" + data.responseText)
                            }
                        })
                    })
                }

                // 普通商品的请求逻辑
                else {
                    $.ajax({
                        type: "POST",
                        url: "/order/create_order?uuidToken=" + uuidToken,
                        data: {
                            "item_id": itemId,
                            "amount": 1,
                            "promoId": details.promoId,
                        }, // todo: amount暂时写死，先模拟秒杀单件商品的情况
                        success: function (data) {
                            if (data.status === "success") {
                                alert("下单成功");
                                window.location.reload()
                            } else {
                                alert(data.data.errorMsg);
                                console.log(data.data.errorCode);
                                if (data.data.errorCode === 10004) {
                                    window.location.href = "/html/login.html"
                                }
                            }
                        },
                        error: function (data) {
                            alert("下单失败：" + data.responseText)
                        }
                    })
                }
            }
        })
    });

    function getId() {
        return window.location.search.split("=")[1];
    }


</script>
</html>
